#!/usr/bin/env python

from PIL import Image
import os
import numpy as np
import sys
import matplotlib.pyplot as plt

WEB_PICS = 'web_pics'
if len(sys.argv) == 1:
    IMG_SRC = 'pants.bmp'
else:
    IMG_SRC = sys.argv[1]

IMG_TARGET = IMG_SRC.split('.')[0] + '_gry' + '.bmp'
CC_TARGET = IMG_SRC.split('.')[0] + '.cc'
print(IMG_TARGET)
FULL_IMG_SRC = os.path.join(WEB_PICS, IMG_SRC)
print(FULL_IMG_SRC)

raw_image = Image.open(FULL_IMG_SRC)
image = raw_image.resize((28, 28))
image_gray = image.convert('L')
print(image_gray)
image_gray_array = np.array(image_gray, dtype=np.uint8)
image_gray_array = 255 - image_gray_array
print(type(image_gray_array))
np.set_printoptions(threshold=sys.maxsize)
person = image_gray_array.reshape(1,28 * 28)
person = np.array(person, dtype=int)
np.savetxt(CC_TARGET, person, fmt='%d', delimiter=',', header="const unsigned char g_person[]={", footer="};", comments='')
#print(person)
#np.save("person.c", person)
print(image_gray_array.shape)

plt.figure()
plt.imshow(image_gray_array, cmap=plt.cm.binary)
im = Image.fromarray(image_gray_array, mode='L')
im.save(IMG_TARGET)
plt.show()
exit()
